﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;


    /// <summary>
    /// 单例模式的基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class Singleton<T> where T : Singleton<T>, new()
    {
        private static T _instance = null;
        private static bool _intialized = false;
        private static bool _created = false;
        private static bool _destroied = false;

        public static T Instance
        {
            get
            {
                if (_destroied == false && _instance == null && _created == false)
                {
                    _instance = new T();
                    _created = true;
                }
                return _instance;
            }
        }

        public static T Create()
        {
            T ret = Instance;
            ret.DoInitialize();
            return ret;
        }

        public static void Destroy()
        {
            if (_instance != null)
            {
                _instance.DoUninitialize();
                _instance = null;
            }
            _created = false;
            //_destroied = true;
        }

        public static void Reset()
        {
            if (_instance != null)
            {
                _instance.OnReset();
            }
        }

        public void DoInitialize()
        {
            if (_intialized == false)
            {
                Initialize();
                _intialized = true;
            }
        }

        public void DoUninitialize()
        {
            if (_intialized == true)
            {
                Uninitialize();
                _intialized = false;
            }
        }

        protected virtual void OnReset() { }

        public virtual void Initialize() { }

        public virtual void Uninitialize() { }

    }

    //Unity单键类
    public class UnitySingleton<T> : MonoBehaviour where T : UnitySingleton<T>
    {
        public static T Instance
        {
            get
            {
                if (instance == null)
                {
                    var objs = FindObjectsOfType(typeof(T));
                    if (objs.Length > 0)
                    {
                        Debug.LogErrorFormat("已经存在{0}个{1}的单例！", objs.Length, typeof(T));
                        instance = (T)objs[0];
                        return instance;
                    }

                    var singleton = new GameObject("(singleton) " + typeof(T));
                    instance = singleton.AddComponent<T>();


#if UNITY_EDITOR
					if (Application.isPlaying)
#endif
                    DontDestroyOnLoad(singleton);

                    Debug.Log("[Singleton] An instance of " + typeof(T) +
                        " is needed in the scene, so '" + singleton +
                        "' was created with DontDestroyOnLoad.");
                }

                return instance;
            }
        }

        public void OnDestroy()
        {
            instance = null;
        }

        static T instance;
    }